package dao.entity;

import bean.Good;
import dao.MysqlConfiguration;
import dao.interf.GoodDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class GoodDaoImpl implements GoodDao {
    @Override
    public List<Good> good_find(int currentPage) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select * from goods limit ?,?;";
        ResultSet rs;
        List<Good> list = new ArrayList<Good>();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,(currentPage-1)*5);
            pst.setInt(2,5);
            rs = pst.executeQuery();
            while (rs.next()){
                Good g = new Good();
                g.setGid(rs.getString(1));
                g.setGname(rs.getString(2));
                g.setIntro(rs.getString(3));
                g.setPrice(Double.parseDouble(rs.getString(4)));
                g.setType(rs.getString(5));
                g.setCcid(rs.getString(6));
                g.setSales(Integer.parseInt(rs.getString(7)));
                g.setGood_form(rs.getString(8));
                g.setMid(rs.getString(9));
                g.setMname(rs.getString(10));
                g.setWeight(Double.parseDouble(rs.getString(11)));
                list.add(g);//添加当前顾客用户到列表中
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return list;
    }

    @Override
    public int good_count() {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select count(*) from goods;";
        ResultSet rs;
        int n = 0;
        try {
            Statement st = conn.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()){
                n = rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败! 错误是:"+e.getMessage());
        }
        return n;
    }

    @Override
    public List<Good> good_findByGname(int currentPage, String gname) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select * from goods where gname like concat('%',?,'%') limit ?,?;";
        ResultSet rs;
        List<Good> list = new ArrayList<Good>();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,gname);
            pst.setInt(2,(currentPage-1)*5);
            pst.setInt(3,5);
            rs = pst.executeQuery();
            while (rs.next()){
                Good g = new Good();
                g.setGid(rs.getString(1));
                g.setGname(rs.getString(2));
                g.setIntro(rs.getString(3));
                g.setPrice(Double.parseDouble(rs.getString(4)));
                g.setType(rs.getString(5));
                g.setCcid(rs.getString(6));
                g.setSales(Integer.parseInt(rs.getString(7)));
                g.setGood_form(rs.getString(8));
                g.setMid(rs.getString(9));
                g.setMname(rs.getString(10));
                g.setWeight(Double.parseDouble(rs.getString(11)));
                list.add(g);//添加当前顾客用户到列表中
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return list;
    }

    @Override
    public List<Good> good_findByMname(int currentPage, String mname) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select * from goods where mname like concat('%',?,'%') limit ?,?;";
        ResultSet rs;
        List<Good> list = new ArrayList<Good>();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,mname);
            pst.setInt(2,(currentPage-1)*5);
            pst.setInt(3,5);
            rs = pst.executeQuery();
            while (rs.next()){
                Good g = new Good();
                g.setGid(rs.getString(1));
                g.setGname(rs.getString(2));
                g.setIntro(rs.getString(3));
                g.setPrice(Double.parseDouble(rs.getString(4)));
                g.setType(rs.getString(5));
                g.setCcid(rs.getString(6));
                g.setSales(Integer.parseInt(rs.getString(7)));
                g.setGood_form(rs.getString(8));
                g.setMid(rs.getString(9));
                g.setMname(rs.getString(10));
                g.setWeight(Double.parseDouble(rs.getString(11)));
                list.add(g);//添加当前顾客用户到列表中
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return list;
    }

    @Override
    public List<Good> good_findByType(int currentPage, String type) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select * from goods where type like concat('%',?,'%') limit ?,?;";
        ResultSet rs;
        List<Good> list = new ArrayList<Good>();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,type);
            pst.setInt(2,(currentPage-1)*5);
            pst.setInt(3,5);
            rs = pst.executeQuery();
            while (rs.next()){
                Good g = new Good();
                g.setGid(rs.getString(1));
                g.setGname(rs.getString(2));
                g.setIntro(rs.getString(3));
                g.setPrice(Double.parseDouble(rs.getString(4)));
                g.setType(rs.getString(5));
                g.setCcid(rs.getString(6));
                g.setSales(Integer.parseInt(rs.getString(7)));
                g.setGood_form(rs.getString(8));
                g.setMid(rs.getString(9));
                g.setMname(rs.getString(10));
                g.setWeight(Double.parseDouble(rs.getString(11)));
                list.add(g);//添加当前顾客用户到列表中
                System.out.println("查询成功!");
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return list;
    }

    @Override
    public ResultSet queryGoodByGid(String gid) {
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select * from goods where gid=?;";
        ResultSet rs = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,gid);
            rs = pst.executeQuery();
        } catch (Exception e) {
            System.out.println("错误!"+e.getMessage());
        }
        return rs;
    }
}
